package com.company.cloud.page.modules.app.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.page.modules.api.models.WechatMomentsSearchModel;
import com.company.cloud.user.modules.api.response.UserSimple;
import com.company.cloud.user.modules.api.service.UserRemoteService;
import com.company.common.server.constant.IRelBusiType;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.company.cloud.behavior.modules.api.response.BehaviorSearchParam;
import com.company.cloud.behavior.modules.api.response.ThumbsUpSearchModel;
import com.company.cloud.behavior.modules.api.service.BehaviorClientService;
import com.company.cloud.common.bean.DataMapEntity;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.data.bean.DataQueryRuleItem;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.page.constant.PageRemoteDataTypeEnum;
import com.company.cloud.page.modules.api.service.PageClientService;
import com.company.cloud.page.modules.app.controller.req.PageDataQuery4PageParam;
import com.company.cloud.page.modules.app.models.PageDataRule;
import com.company.cloud.page.modules.app.models.PageElementRuleModel;
import com.company.cloud.page.modules.app.service.PageDataService;
import com.company.cloud.page.modules.base.entity.PageConfigInfo;
import com.company.cloud.page.modules.base.service.PageConfigInfoService;
import com.company.cloud.product.modules.api.request.ProductQuery4PageRequest;
import com.company.cloud.product.modules.api.request.ProductQueryRequest;
import com.company.cloud.product.modules.api.response.ProductQuery4PageResponse;
import com.company.cloud.product.modules.api.service.ProductRemoteService;

@Log4j2
@Service
public class PageDataServiceImpl implements PageDataService {

	@Autowired
	private ProductRemoteService productRemoteService;
	@Autowired
	private PageConfigInfoService pageConfigInfoService;
	@Autowired
	private BehaviorClientService behaviorClientService;
	@Qualifier("pageClientServiceImpl")
	@Autowired
	private PageClientService pageClientService;
	@Autowired
	private UserRemoteService userRemoteService;

	@Override
	public DataMapEntity getPageData(String companyNo, String pageNo, Integer pageUserId,
			List<PageElementRuleModel> rules,LoginSimpleUserInfo userInfo) {
		DataMapEntity data = new DataMapEntity();
		for (PageElementRuleModel item : rules) {
			if (item != null && StringUtils.hasText(item.getKey()) && item.getDataRule() != null) {
				this.getElementData(data, companyNo, pageNo, pageUserId, item.getKey(), item.getDataRule(),userInfo);
			}
		}
		return data;
	}

	private void getElementData(DataMapEntity data, String companyNo, String pageNo, Integer pageUserId, String key,
			PageDataRule dataRule,LoginSimpleUserInfo userInfo) {
		if (PageRemoteDataTypeEnum.PRD_Product.getCode().equals(dataRule.getRemoteFunc())) {
			Object prdData = this.getPrd(companyNo, dataRule.getRemoteOption());
			if (prdData != null) {
				data.put(key, prdData);
			}
		}

		if (PageRemoteDataTypeEnum.PRD_WechatMoments.getCode().equals(dataRule.getRemoteFunc())) {
			Object wechatMomentsData = this.getWechatMoments(pageUserId);
			if (wechatMomentsData != null) {
				data.put(key, wechatMomentsData);
			}
		}

		if (PageRemoteDataTypeEnum.PRD_ThumbsUp.getCode().equals(dataRule.getRemoteFunc())) {
			Object thumbsUpData = this.getThumbsUp(pageNo,userInfo);
			if (thumbsUpData != null) {
				data.put(key, thumbsUpData);
			}
			//this.getThumbsUp(pageNo);
		}

		if (PageRemoteDataTypeEnum.PRD_UserInfo.getCode().equals(dataRule.getRemoteFunc())) {
			Object user= this.getUserInfo(pageUserId);
			if (user != null) {
				data.put(key, user);
			}
		}

//		if (PageRemoteDataTypeEnum.PRD_UserHealthInfo.getCode().equals(dataRule.getRemoteFunc())) {
//			Object healthBook=this.getHealthBook(pageUserId);
//			if (healthBook != null) {
//				data.put(key, healthBook);
//			}
//		}

		if (PageRemoteDataTypeEnum.PRD_Score.getCode().equals(dataRule.getRemoteFunc())) {

		}


	}

	/** 商品 */
	private Object getPrd(String companyNo, List<DataQueryRuleItem> remoteOption) {
		ProductQueryRequest request = new ProductQueryRequest();
		request.setCompanyNo(companyNo);
		request.setTop(100);
		request.setItems(remoteOption);
//		System.out.println("++++++++++++++++++商品");
//		System.out.println(JSON.toJSONString(request));
		Object prdData = productRemoteService.queryProductSimple(request);
		return prdData;
	}

	/** 点赞 */
	private Object getThumbsUp(String pageNo,LoginSimpleUserInfo userInfo) {
		log.debug("+++++名片:点赞 ");
		BehaviorSearchParam behaviorSearchParam = new BehaviorSearchParam();
		if(userInfo==null){
			behaviorSearchParam.setUserId(0);
		}else{
			behaviorSearchParam.setUserId(userInfo.getId());
		}
		behaviorSearchParam.setObjType(IRelBusiType.CMS_PageCard);
		behaviorSearchParam.setObjNo(pageNo);
		behaviorSearchParam.setUserTop(4);
		ThumbsUpSearchModel data = behaviorClientService.getThumbsUpUserInfo(behaviorSearchParam).getData();
		if (data == null) {
			return null;
		}
		List<ThumbsUpSearchModel> obj = new ArrayList<ThumbsUpSearchModel>();
		obj.add(data);
		return obj;
	}

	/** 动态 */
	private Object getWechatMoments(Integer userId) {
		log.debug("+++++名片:动态 ");
		if(userId==null) {
			return null;
		}
		WechatMomentsSearchModel param = new WechatMomentsSearchModel();
		param.setUserId(userId);
		param.setTop(20);
		return pageClientService.getWechatMoments2List(param);
	}

	/**
	 * 产品分页
	 * */
	private Object getPrd4Page(String companyNo, List<DataQueryRuleItem> remoteOption, int page, int rows) {
		ProductQuery4PageRequest request = new ProductQuery4PageRequest();
		request.setCompanyNo(companyNo);
		request.setItems(remoteOption);
		request.setPage(page);
		request.setRows(rows);
		BaseClientResult<ProductQuery4PageResponse> prdData = productRemoteService.queryProductSimple4Page(request);
		CheckUtils.checkClientResult(prdData);
		return prdData;
	}

	/**
	 * 用户信息
	 * */
	private Object getUserInfo(Integer userId){
		log.debug("+++++名片:用户信息 ");
		UserSimple userSimple=userRemoteService.getUserById(userId).getData();
		return userSimple;
	}



	/**
	 * 评分信息
	 * */
	@Override
	public Object queryPageEleData(PageDataQuery4PageParam params, LoginSimpleUserInfo userInfo) {
		PageConfigInfo pageConfig = pageConfigInfoService.getById(params.getPageNo());
		if (pageConfig == null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		if (PageRemoteDataTypeEnum.PRD_Product.getCode().equals(params.getRemoteFunc())) {
			return this.getPrd4Page(pageConfig.getCompanyNo(), params.getRemoteOption(), params.getPage(),
					params.getRows());
		}
		
		if (PageRemoteDataTypeEnum.PRD_WechatMoments.getCode().equals(params.getRemoteFunc())) {
		}
		

		if (PageRemoteDataTypeEnum.PRD_ThumbsUp.getCode().equals(params.getRemoteFunc())) {
		}
		
		
		return null;

	}

}
